<template>
	<el-main class="bg">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
			<el-col
				v-if="user_group === '管理员' || $check_field('get', 'event_name') || $check_field('add', 'event_name') || $check_field('set', 'event_name')"
				:xs="24" :sm="12" :lg="8">
				<el-form-item label="赛事名称" prop="event_name">
					<el-select id="event_name" v-model="form['event_name']" placeholder="请选择赛事名称" v-if="user_group === '管理员'"
						:disabled="disabledObj['event_name_isDisabled']" @change="fillEventInfo">
						<el-option v-for="(name, index) in eventList" :key="index" :label="name" :value="name"></el-option>
					</el-select>
					<div v-else-if="$check_field('get', 'event_name')">{{ form['event_name'] }}</div>
				</el-form-item>
			</el-col>
			<el-col
				v-if="user_group === '管理员' || $check_field('get', 'college_name') || $check_field('add', 'college_name') || $check_field('set', 'college_name')"
				:xs="24" :sm="12" :lg="8">
				<el-form-item label="院系" prop="college_name">
					<el-select id="college_name" v-model="form['college_name']" placeholder="请选择院系"
						v-if="user_group === '管理员' || (form['team_score_id'] && $check_field('set', 'college_name')) || (!form['team_score_id'] && $check_field('add', 'college_name'))"
						:disabled="disabledObj['college_name_isDisabled']" @change="filterTeamsByCollege">
						<el-option v-for="college in college_list" :key="college" :label="college" :value="college"></el-option>
					</el-select>
					<div v-else-if="$check_field('get', 'college_name')">{{ form['college_name'] }}</div>
				</el-form-item>
			</el-col>
			<el-col
				v-if="user_group === '管理员' || $check_field('get', 'score') || $check_field('add', 'score') || $check_field('set', 'score')"
				:xs="24" :sm="12" :lg="8">
				<el-form-item label="得分" prop="score">
					<el-input id="score" v-model="form['score']" placeholder="请输入得分"
						v-if="user_group === '管理员' || (form['individual_score_id'] && $check_field('set', 'score')) || (!form['individual_score_id'] && $check_field('add', 'score'))"
						:disabled="disabledObj['score_isDisabled']"></el-input>
					<div v-else-if="$check_field('get', 'score')">{{ form['score'] }}</div>
				</el-form-item>
			</el-col>
			<el-col
				v-if="user_group === '管理员' || $check_field('get', 'judge_comment') || $check_field('add', 'judge_comment') || $check_field('set', 'judge_comment')"
				:xs="24" :sm="12" :lg="8">
				<el-form-item label="评委评语" prop="judge_comment">
					<el-input id="judge_comment" v-model="form['judge_comment']" placeholder="请输入评委评语"
						v-if="user_group === '管理员' || (form['individual_score_id'] && $check_field('set', 'judge_comment')) || (!form['individual_score_id'] && $check_field('add', 'judge_comment'))"
						:disabled="disabledObj['judge_comment_isDisabled']"></el-input>
					<div v-else-if="$check_field('get', 'judge_comment')">{{ form['judge_comment'] }}</div>
				</el-form-item>
			</el-col>
			<el-col
				v-if="user_group === '管理员' || $check_field('get', 'user_name') || $check_field('add', 'user_name') || $check_field('set', 'user_name')"
				:xs="24" :sm="12" :lg="8">
				<el-form-item label="用户姓名" prop="user_name">
					<el-select v-model="form['user_name']" :disabled="disabledObj['user_name_isDisabled']">
						<el-option v-for="o in list_user_name" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
							:value="o['user_id']">
						</el-option>
					</el-select>
				</el-form-item>
			</el-col>
			<el-col :xs="24" :sm="12" :lg="8">
				<el-form-item>
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
			</el-col>
		</el-form>
	</el-main>
</template>
<script>
import mixin from "@/mixins/page.js";

export default {
	mixins: [mixin],
	data() {
		return {
			college_list: ['计算机学院', '材料与食品学院', '经贸学院', '管理学院', '外国语学院', '电子信息学院', '机电工程学院', '艺术设计学院', '人文社会科学学院', '继续教育学院'],
			field: "individual_score_id",
			url_add: "~/api/individual_competition_score/add?",
			url_set: "~/api/individual_competition_score/set?",
			url_get_obj: "~/api/individual_competition_score/get_obj?",
			query: {
				"individual_score_id": 0,
			},
			form: {
				"event_name": '', // 赛事名称
				"score": '', // 得分
				"judge_comment": '', // 评委评语
				"user_name": 0, // 用户姓名
				"individual_score_id": 0, // ID
			},
			disabledObj: {
				"event_name_isDisabled": false,
				"score_isDisabled": false,
				"judge_comment_isDisabled": false,
				"user_name_isDisabled": false,
			},
			list_user_name: [], // 用户姓名列表

			eventList: [],
			fullEventList: [],
		}
	},

	methods: {
		async get_event_type() {
			console.log(1111);

			var json = await this.$get("~/api/event_application/get_list?like=0");
			console.log(json.result.list);
			this.fullEventList = [];
			if (json.result && json.result.list) {
				json.result.list.forEach(item => {
					if (item.examine_state === "已通过") {
						this.eventList.push(item.event_name);
						this.fullEventList.push(item);
					}
				});
			}
			else if (json.error) {
				console.error(json.error);
			}
		},
		// 填充赛事类型和赛事编号
		fillEventInfo() {
			const selectedEvent = this.fullEventList.find(item => item.event_name === this.form['event_name']);
			if (selectedEvent) {
				this.form['event_type'] = selectedEvent.event_type;
				this.form['event_number'] = selectedEvent.event_number;
			} else {
				this.form['event_type'] = '';
				this.form['event_number'] = '';
			}
		},
		/**
		 * 获取用户姓名列表
		 */
		async get_list_user_name() {
			var json = await this.$get("~/api/user/get_list?user_group=普通用户");
			if (json.result && json.result.list) {
				this.list_user_name = json.result.list;
			}
			else if (json.error) {
				console.error(json.error);
			}
		},
		get_user_user_name(id) {
			var obj = this.list_user_name.find(o => o.user_id === id);
			return obj ? obj.nickname + "-" + obj.username : "";
		},
		is_view() {
			var bl = this.user_group == "管理员";
			if (!bl) {
				bl = this.$check_action('/individual_competition_score/table', 'add');
			}
			if (!bl) {
				bl = this.$check_action('/individual_competition_score/table', 'set');
			}
			if (!bl) {
				bl = this.$check_action('/individual_competition_score/view', 'add');
			}
			if (!bl) {
				bl = this.$check_action('/individual_competition_score/view', 'set');
			}
			if (!bl) {
				bl = this.$check_action('/individual_competition_score/view', 'get');
			}
			return bl;
		},
	},
	created() {
		this.get_list_user_name();

		this.get_event_type();
	}
}
</script>
<style>
.bg {
	background: white;
}
</style>